package com.yunquan.ohana.ui.calendar.event.week

import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import com.yunquan.ohana.BR
import com.yunquan.ohana.R
import com.yunquan.ohana.base.ui.ItemViewModel
import com.yunquan.ohana.ui.calendar.event.week.adapter.EventWeekChildAdapter
import me.tatarka.bindingcollectionadapter2.ItemBinding
import java.time.LocalDate
import java.time.format.TextStyle
import java.util.Locale

class EventWeekItemViewModel(viewModel: EventWeekViewModel) : ItemViewModel<EventWeekViewModel>(viewModel) {

    val adapter = EventWeekChildAdapter()
    val items = ObservableArrayList<EventWeekChildItemViewModel>()
    val itemBinding = ItemBinding.of<EventWeekChildItemViewModel>(
        BR.eventWeekChildItemViewModel,
        R.layout.item_event_week,
    )

    var date: String? = null

    val week = ObservableField<String>()
    val day = ObservableField<String>()

    constructor(
        viewModel: EventWeekViewModel,
        date: String,
        child: List<EventWeekChildItemViewModel>,
    ) : this(viewModel) {
        this.date = date
        LocalDate.parse(date).let {
            week.set(it.dayOfWeek.getDisplayName(TextStyle.SHORT, Locale.getDefault()))
            day.set(it.dayOfMonth.toString())
        }
        setData(child)
    }

    fun setData(child: List<EventWeekChildItemViewModel>) {
        this.items.clear()
        this.items.addAll(child)
    }

}